package com.vastcom.springbootangular5.security.auth;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.vastcom.springbootangular5.model.User;
import com.vastcom.springbootangular5.model.UserTokenState;
import com.vastcom.springbootangular5.security.TokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    @Value("${jwt.expires_in}")
    private int EXPIRES_IN;

    @Value("${jwt.cookie}")
    private String TOKEN_COOKIE;

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    TokenHelper tokenHelper;


    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        clearAuthenticationAttributes(request);
        User user=(User) authentication.getPrincipal();
        String jws=tokenHelper.generateToken(user.getUsername());

        //create token auth cookie

        Cookie authCookie=new Cookie(TOKEN_COOKIE,(jws));

        authCookie.setHttpOnly(true);
        authCookie.setMaxAge(EXPIRES_IN);
        authCookie.setPath("/");
        response.addCookie(authCookie);

        UserTokenState userTokenState=new UserTokenState(jws,EXPIRES_IN);
         String jwtResponse=objectMapper.writeValueAsString(userTokenState);
         response.setContentType("application/json");
         response.getWriter().write(jwtResponse);



    }
}
